#ifndef FTPCLIENTMAINWINDOW_H
#define FTPCLIENTMAINWINDOW_H

#include "ftpclientui_global.h"
#include <QMainWindow>
#include <QFileInfo>
#include <QMap>
#include <QModelIndex>
#include <QSortFilterProxyModel>

namespace Ui {
class FtpClientMainWindow;
}

class FtpClient;
class QTableWidget;

// 在 FtpClientMainWindow 类前添加自定义排序代理类
class FileSizeSortProxyModel : public QSortFilterProxyModel {
    Q_OBJECT
public:
    FileSizeSortProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}

    void setFileSizeMap(const QMap<QString, qint64>* fileSizeMap, bool isRemote = false) {
        m_fileSizeMap = fileSizeMap;
        m_isRemote = isRemote;
    }

    void setLocalFileTable(QTableWidget* tableWidget) {
        m_localFileTable = tableWidget;
    }

protected:
    bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override {
        // 如果排序的是大小列（索引为2）
        if (source_left.column() == 2) {
            qint64 sizeLeft = getActualFileSize(source_left);
            qint64 sizeRight = getActualFileSize(source_right);
            return sizeLeft < sizeRight;
        }

        // 其他列使用默认排序
        return QSortFilterProxyModel::lessThan(source_left, source_right);
    }

private:
    qint64 getActualFileSize(const QModelIndex& index) const;
    qint64 parseFileSize(const QString& sizeText) const;

    const QMap<QString, qint64>* m_fileSizeMap = nullptr;
    bool m_isRemote = false;
    QTableWidget* m_localFileTable = nullptr;
    QString m_currentLocalPath; // 需要在初始化时设置
};

class FTPCLIENTUI_EXPORT FtpClientMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit FtpClientMainWindow(QWidget *parent = nullptr);
    ~FtpClientMainWindow();
    
    void loadSettings(const QString &iniFileName, const QString &groupName);
    void saveSettings(const QString &iniFileName, const QString &groupName);

private slots:
    // UI交互相关槽函数
    void onConnectButtonClicked();
    void onDisconnectButtonClicked();
    void onListButtonClicked();
    void onDownloadButtonClicked();
    void onUploadButtonClicked();
    void onSelectLocalPathButtonClicked();
    
    // 本地文件相关槽函数
    void onLocalFileTableContextMenuRequested(const QPoint &pos);
    void onUploadActionTriggered();
    void onDeleteLocalActionTriggered(); // 处理本地文件删除
    void onRefreshLocalActionTriggered(); // 处理本地文件刷新
     // 远程文件相关槽函数
    void onRemoteFileTableContextMenuRequested(const QPoint &pos);
    void onLocalTableHeaderDoubleClicked(int logicalIndex);
    void onRemoteTableHeaderDoubleClicked(int logicalIndex);
    void onDownloadActionTriggered();
    void onDeleteActionTriggered();
    void onRefreshRemoteActionTriggered();
    
    // FTP客户端信号处理槽函数
    void onFtpConnected(bool success, const QString &message);
    void onFtpDisconnected();
    void onFileListReceived(bool success, const QList<QStringList>& fileList, const QString& message);
    void onDownloadProgress(qint64 bytesRead, qint64 totalBytes);
    void onDownloadFinished(bool success, const QString &fileName, const QString &message);
    void onUploadProgress(qint64 bytesSent, qint64 totalBytes);
    void onUploadFinished(bool success, const QString &fileName, const QString &message);
    void onDeleteFinished(bool success, const QString &fileName, const QString &message);
    void onFtpError(const QString &message);
    void onDirectoryChanged(bool success, const QString &directory, const QString &message);
    void onRemoteFileCellDoubleClicked(int row, int column);
protected:
    void showEvent(QShowEvent* event) override;
    void closeEvent(QCloseEvent* event) override;
private:
    Ui::FtpClientMainWindow *ui;
    FtpClient *m_ftpClient;
    
    // 右键菜单相关
    QMenu *m_remoteContextMenu;
    QMenu *m_localContextMenu;
    QAction *m_downloadAction;
    QAction *m_uploadAction;
    QAction *m_deleteAction;
    QAction* m_refreshRemoteAction;
    QAction *m_deleteLocalAction; // 本地文件删除动作
    QAction *m_refreshLocalAction; // 本地文件刷新动作
    QString m_currentRemoteFile;
    QString m_currentLocalFile;
    QMap<QString, qint64> m_fileSizeMap;
    
    // 本地文件管理
    void refreshLocalFileList(const QString &path = ".");
    void setupMenus();
    void setupConnections();

    QString m_iniFile;
    QString m_groupName;
    quint16 m_port;
#define FILE_LIST_COLUMNS 4
    Qt::SortOrder localColOrder[FILE_LIST_COLUMNS];
    Qt::SortOrder remoteColOrder[FILE_LIST_COLUMNS];

    FileSizeSortProxyModel* m_localFileSortProxy;
    FileSizeSortProxyModel* m_remoteFileSortProxy;
    QString m_currentLocalPath; // 当前本地路径，用于获取文件实际大小
};

#endif // FTPCLIENTMAINWINDOW_H
